<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 后台首页
 */
class Main extends CI_Controller{
    public function __construct()	{
        parent::__construct();
        $this->load->helper('url');
        $this->load->helper('form');
        $token = $this->input->cookie('token');
        $this->load->library('Auth');
        $this->data['admin_user'] = $this->admin_user = Auth::get_admin_user_by_token($token);
        if(empty($this->data['admin_user'])){
            redirect('/login', 'refresh');
        }
        $soft_id = $this->input->cookie('soft_id');
        $soft_id = empty($soft_id)?1:$soft_id;
        $this->data['soft_id'] = $this->soft_id =$soft_id;
    }
    /**
     * 后台主界面
     * 判断是否登录，如果没有登录，跳转到登录页面
     * 获取用户权限列表，生成左侧菜单。
     */
    public function index(){
        $this->load->model('admin/soft_model');
        $this->data['soft_list'] = $this->soft_model->get_soft();
        $this->get_menu_data($this->soft_id);
        $this->load->view('admin/main/index',$this->data);
	}

    /**
     * 更换左侧菜单
     * @param int $soft_id
     */
    public function menu($soft_id){
        $this->input->set_cookie('soft_id',$soft_id,0);
        $this->get_menu_data($soft_id);
        $this->load->view('admin/main/menu',$this->data);
    }

    /**
     * 获取指定软件的左侧菜单数据
     */
    private function get_menu_data($soft_id){
        $is_admin = Auth::is_administrator($this->admin_user['username']); //是否是超级管理员
        $this->load->model('admin/admin_node_model');
        $this->load->model('admin/admin_user_model');
        $this->load->model('admin/admin_node_group_model');
        if($is_admin){
            $admin_node_menu = $this->admin_node_model->get_admin_node_menu();//是超级管理员：获取所有可显示在左侧菜单的权限节点
        }else{
            $admin_node_menu = $this->admin_user_model->get_admin_user_auth($this->admin_user['id'],$soft_id,false);//获取所有可用显示在左侧菜单的权限节点
        }
        $this->data['admin_node_group_menu'] = $this->admin_node_group_model->get_admin_node_group_menu($admin_node_menu);
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */